package com.pro.system.web.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.system.annation.CurrentUser;
import com.pro.system.auto.entity.User;
import com.pro.system.service.UserService;
import com.pro.system.utils.Result;

/***
 * 用户注册
 * @author 高元东
 *
 */
@Controller
@RequestMapping("/reg")
public class RegUserController {

	@Autowired
	private UserService userService;
	private Logger logger = Logger.getLogger(RegUserController.class);
	
	
	@RequestMapping(value = "/doReg")
	@ResponseBody
	public Result doRegUser(User user) {
		Result result = new Result();
		logger.info("注册新用户  userName :" +user.getUserName() +" email :" + user.getEmail());
		try {
			this.userService.regUser(user);
			logger.info("注册成功!");
			result.setSuccess(true);
			result.setMessage("注册成功");
		} catch (Exception e) {
			logger.info("注册失败!" + e);
			result.setSuccess(false);
			result.setMessage("注册失败，请重试!");
			e.printStackTrace();
		}
		return result;
		
	}
	
	@RequestMapping("/checkUserName")
	@ResponseBody
	public Result checkUserName(String userName) {
		Result result = new Result();
		boolean has  = this.userService.checkUser(userName);
		if (has) {
			
			result.setSuccess(true);
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	
	@RequestMapping("/checkEmail")
	@ResponseBody
	public Result checkEmail(String email) {
		Result result = new Result();
		boolean has  = this.userService.checkEmail(email);
		if (has) {
			
			result.setSuccess(true);
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	@RequestMapping("/checkNickName")
	@ResponseBody
	public Result checkNickName(String nickName) {
		Result result = new Result();
		boolean has  = this.userService.checkNickName(nickName);
		if (has) {
			
			result.setSuccess(true);
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	/***
	 * 检查手机号码是否被占用
	 * @param mobile
	 * @return
	 */
	@RequestMapping("/checkMobile")
	@ResponseBody
	public Result checkMobile(String mobile) {
		Result result = new Result();
		boolean has  = this.userService.checkMobile(mobile);
		if (has) {
			
			result.setSuccess(true);
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	
}
